<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="小区" prop="areaName">
        <el-select v-model="queryParams.areaName" :filter-method="filterMethod" allow-create clearable
                   default-first-option filterable
                   placeholder="请输入小区" size="small" @change="getGroup(queryParams.areaName)">
          <el-option v-for="(item, index) in areaBindIdOptions" :key="index" :disabled="item.disabled"
                     :label="item.areaName" :value="item.areaName"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="楼宇" prop="groupName">
        <el-select v-model="queryParams.groupName" clearable placeholder="请输入楼宇" size="small"
                   @change="getUnit(queryParams.areaName,queryParams.groupName)">
          <el-option v-for="(item, index) in groupBindIdOptions" :key="index" :disabled="item.disabled"
                     :label="item.groupName" :value="item.groupName"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="单元" prop="unitName">
        <el-select v-model="queryParams.unitName" clearable placeholder="请输入单元" size="small">
          <el-option v-for="(item, index) in unitBindNameOptions" :key="index" :disabled="item.disabled"
                     :label="item.unitName" :value="item.unitName"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="户" prop="doorName">
        <el-input
          v-model="queryParams.doorName" clearable
          placeholder="请输入户名称"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="userName">
        <el-input
          v-model="queryParams.userName" clearable
          placeholder="请输入姓名"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          icon="el-icon-upload"
          plain
          size="small"
          type="warning"
          @click="handleImport"
        >导入
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="doorBindQueryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="小区名称" align="center" prop="areaName"/>
      <el-table-column label="楼宇名称" align="center" prop="groupName"/>
      <el-table-column label="单元名称" align="center" prop="unitName"/>
      <el-table-column label="户名称" align="center" prop="doorName"/>
      <el-table-column label="房屋编号" align="center" prop="houseId"/>
      <el-table-column label="面积" align="center" prop="space"/>
      <el-table-column label="姓名" align="center" prop="userName" width="200"/>
      <el-table-column label="电话" align="center" prop="tel" width="200"/>
      <el-table-column align="center" label="合并状态" prop="bindSts">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.HOUSE025" :value="scope.row.bindSts"/>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px">
      <el-upload
        ref="upload"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :auto-upload="false"
        :disabled="upload.isUploading"
        :headers="upload.headers"
        :limit="1"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        accept=".xlsx"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div slot="tip" class="el-upload__tip" style="color:red">提示：仅允许导入“xlsx”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="cancelFileForm">取 消</el-button>

      </div>
    </el-dialog>

  </div>
</template>

<script>
import {listDoorBindQuery,getAreaBindInfo, getAreaBindInfo1,getGroupBindInfo,getUnitBindInfo} from "@/api/fund/doorBindQuery";
import {getToken} from "@/utils/auth";

export default {
  name: "DoorBindQuery",
  dicts: ['HOUSE025'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 户绑定表格数据
      doorBindQueryList: [],
      areaBindIdOptions: [{}],
      groupBindIdOptions: [{}],
      unitBindNameOptions: [{}],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        areaName: null,
        groupName: null,
        unitName: null,
        doorName: null,
      },
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: {Authorization: "Bearer " + getToken()},
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/fund/doorBindQuery/importData"
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
    this.areaSelect();
  },
  methods: {
    filterMethod(val) {
      this.value = val
      getAreaBindInfo1(this.value).then(response => {
        this.areaBindIdOptions = response.rows;
      });
    },
    areaSelect() {
      getAreaBindInfo().then(response => {
        this.areaBindIdOptions = response.rows;
      });
    },
    getGroup(areaName) {
      if (areaName != "") {
        getGroupBindInfo(areaName).then(response => {
          this.groupBindIdOptions = response.rows;
        });
      }
    },
    getUnit(areaName, groupName) {
      getUnitBindInfo(areaName, groupName).then(response => {
        this.unitBindNameOptions = response.rows;
      });
    },
    /** 查询户绑定列表 */
    getList() {
      this.loading = true;
      listDoorBindQuery(this.queryParams).then(response => {
        this.doorBindQueryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },

    cancelFileForm() {
      this.upload.open = false;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", {dangerouslyUseHTMLString: true});
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
  }
};
</script>
